//index.js
const app = getApp()
let db = wx.cloud.database();
const _ = db.command

Page({
  data: {
    longitude: null,
    latitude: null,
    scale: 15,

    cur: null,
    points:[],
    markers: [],
    autoplay: true,
    refresh: false
  },



  onLoad: function () {


  },
  getLocation: function (callback) {
    wx.getLocation({
      type: 'gcj02',
      success(res) {

        callback({
          "latitude": res.latitude,
          "longitude": res.longitude
        });
      }
    })
  },

  getPromotes: function () {
    var that = this;
    var ds = null;

    db.collection('videos').orderBy('time', 'desc')
      .where({
        location: _.geoNear({
          geometry: db.Geo.Point(that.data.longitude, that.data.latitude),
          minDistance: 0,
          maxDistance: 5000,
        })
      })
      .get({
        success: function (res) {

          ds = res.data;

          var result = [];
          var idf = 0;

          var flag = false;
          for (let item in ds) {

            idf = idf + 1;

            var marker = {
              id: idf,

              latitude: ds[item]["location"]["latitude"],
              longitude: ds[item]["location"]["longitude"],
              title: ds[item]["title"],
              iconPath: '../../images/video.png',
              src: ds[item]["src"],
              width: 32,
              height: 32
            };

            if (!flag) {

              var curv = marker;

              that.setData({
                cur: curv
              })
              flag = true;
            }

            result.push(marker);
          }

          that.setData({
            markers: result,
            refresh: true
          })
        }
      })


  },
  inte: null,
  onReady: function () {
      var that = this;
      this.inte = setInterval(function () {
        that.getLocation(res => {
          that.setData({
            longitude: res.longitude,
            latitude: res.latitude,

          });
          // that.data.points.push({
          //   "longitude": res.longitude,
          //   "latitude": res.latitude
          // });
          app.globalData.longitude = res.longitude;
          app.globalData.latitude = res.latitude;
          that.getPromotes(that);


        });
      }, 15000);
    }

    // line: function (start,end) {
    //   var _this = this
    //   qqmapsdk.direction({
    //     mode: 'driving',
    //     from: start,
    //     to: end,
    //     success: function (res,data) {
    //       console.log(res);
    //       var ret = res;
    //       var coors = ret.result.routes[0].polyline, pl = [];
    //       //坐标解压（返回的点串坐标，通过前向差分进行压缩）
    //       var kr = 1000000;
    //       for (var i = 2; i < coors.length; i++) {
    //         coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
    //       }
    //       //将解压后的坐标放入点串数组pl中
    //       for (var i = 0; i < coors.length; i += 2) {
    //         pl.push({ latitude: coors[i], longitude: coors[i + 1] })
    //       }
    //       console.log(pl)
    //       //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
    //       _this.setData({
    //         latitude: pl[0].latitude,
    //         longitude: pl[0].longitude,
    //         polyline: [{
    //           points: pl,
    //           color: "#37C4FF",
    //            width: 4, 
    //            dottedLine: false
    //         }]
    //       })
    //     },
    //     fail: function (error) {
    //       console.error(error);
    //     },
    //     complete: function (res) {
    //       console.log(res);
    //     }
    //   });
    // },
    ,
  markertap: function (e) {
    let id = e.detail["markerId"];
    console.log('id', id)
    console.log('this.data.markers', this.data.markers);
    let marker = this.data.markers[id - 1];
    console.log(marker)

    var curv = marker;
    console.log(curv);
    clearInterval(inte);
    this.setData({
      cur: curv
    })
  }

})